package com.zs.usc.utils;

import java.util.UUID;

/**
 * UUID帮助类
 */
public class UUIDUtils {

    private UUIDUtils() {
    }

    /**
     * 生成去掉“-”的UUID
     *
     * @return UUID字符串
     */
    public static String generate() {
        UUID uuid = UUID.randomUUID();
        String primaryKey = uuid.toString().replaceAll("-", "");
        return primaryKey;
    }

    /**
     * 生成指定长度的UUID
     *
     * @param length UUID长度
     * @return UUID字符串
     */
    public static String generate(int length) {
        if (length <= 32) {
            return generate().substring(0, length);
        } else if (length > 32 && length <= 36) {
            return UUID.randomUUID().toString().substring(0, length);
        } else {
            int count = length / 32;
            int remain = length % 32;
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < count; ++i) {
                builder.append(generate());
            }

            if (remain > 0) {
                builder.append(generate().substring(0, remain));
            }

            return builder.toString();
        }
    }
}
